package impl

import (
	"context"
	"github.com/infraboard/mcube/sqlbuilder"
	"github.com/infraboard/mcube/types/ftime"
	"github.com/rs/xid"
	"mygoedu/day14/demo/api/pkg/host"
)

const (
	insertResourceSQL = `INSERT into resource (id,vendor,zone,create_at,expire_at,category,type,instance_id,name,
description,status,update_at,sync_at,sync_accout,public_ip,private_ip,pay_type,describe_hash,resource_hash) 
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
	insertHostSQL = `INSERT into host (resource_id,cpu, memory,gpu_amount,gpu_spec,os_type,os_name,serial_number,
image_id,internet_max_bandwidth_out,internet_max_bandwidth_in,key_pair_name,security_groups) 
values (?,?,?,?,?,?,?,?,?,?,?,?,?)`
	updateHostSQL = `UPDATE resource set expire_at = ?,category =?,type = ?,name = ?,description = ?,status = ?,
update_at = ?,sync_at = ?,sync_accout = ?,public_ip = ?,private_ip = ?,pay_type = ?,describe_hash = ?,resource_hash = ?
WHERE id = ?`
	queryHostSQL      = `SELECT * FROM resource as r LEFT JOIN host h ON r.id=h.resource_id`
	deleteHostSQL     = `DELETE FROM host WHERE resource_id = ?;`
	deleteResourceSQL = `DELETE FROM resource WHERE id = ?;`
)

func (s *service) SaveHost(ctx context.Context, h *host.Host) (*host.Host, error) {
	h.Id = xid.New().String()
	h.ResourceId = h.Id
	h.SyncAt = ftime.Now().Timestamp()

	if err := s.save(ctx, h); err != nil {
		return nil, err
	}
	return h, nil
}

func (s *service) QueryHost(ctx context.Context, req *host.QueryHostRequest) (*host.HostSet, error) {
	query := sqlbuilder.NewQuery(queryHostSQL)
	querySQL, args := query.Order("sync_at").Desc().Limit(req)
}

func (s *service) UpdateHost(ctx context.Context, host *host.UpdateHostRequest) (*host.Host, error) {

}

func (s *service) DescribeHost(ctx context.Context, host *host.DescribeHostRequest) (*host.Host, error) {

}
